package com.love_weinuo.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.lzy.okgo.callback.StringCallback;
import com.love_weinuo.R;
import com.love_weinuo.databinding.ActivityForgetBinding;
import com.love_weinuo.ui.BaseActivity;
import com.love_weinuo.util.HTTPAPI;
import com.love_weinuo.util.ToastUtil;
import com.love_weinuo.util.timeutil.RegisterTime;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by SensYang on 2019/02/28 12:30
 */

public class ForgetActivity extends BaseActivity {

    ActivityForgetBinding binding;
    RegisterTime timeUtil;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_forget);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        add(this);
        String style = getIntent().getStringExtra("fromStyle");
        if ("change".equals(style)) {
            setTitleText("修改密码");
        } else {
            setTitleText("忘记密码");
        }
        setBack();
        binding.textViewCode.setOnClickListener(this);
        binding.textViewUpdata.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.textViewCode:
                String phone = binding.phone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showLongToast("请输入您的手机号码");
                    return;
                } else {
                    timeUtil = new RegisterTime(60000, 1000, binding.textViewCode);
                    timeUtil.start();
                    getcode(phone);
                }
                break;
            case R.id.textViewUpdata:
                String phoneUpdada = binding.phone.getText().toString().trim();
                if (TextUtils.isEmpty(phoneUpdada) || phoneUpdada.length() != 11) {
                    showInCommonDialog("请输入格式正确的手机号码");
                    binding.phone.setText("");
                    return;
                }

                String code = binding.code.getText().toString().trim();
                if (TextUtils.isEmpty(code) || code.length() != 6) {
                    ToastUtil.showToast("请输入格式正确的验证码");
                    return;
                }
                Intent intent = new Intent(ForgetActivity.this, ForgetToInputPwdActivity.class);
                intent.putExtra("phone", phoneUpdada);
                intent.putExtra("code",code);
                startActivity(intent);
                break;
            default:
        }
    }


    public void getcode(String phone) {
        HTTPAPI.getInstance().getForgetCode(phone, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                showInCommonDialog("验证码发送成功");
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                showInCommonDialog("验证码发送失败");
            }
        });
    }
}
